package com.star.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.star.security.annotations.Authorize;
import com.star.system.dto.RoleSaveDTO;
import com.star.system.dto.RoleUpdateDTO;
import com.star.system.service.MenuService;
import com.star.system.service.RoleService;
import com.star.system.vo.MenuRoleTreeVO;
import com.star.system.vo.RoleDetailVO;
import com.star.system.vo.RolePageVO;
import com.star.system.vo.RoleSimpleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色
 *
 * @author yangzj
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    /**
     * 保存
     *
     * @param dto dto
     */
    @Authorize("role:save")
    @PostMapping
    public void save(@Validated @RequestBody RoleSaveDTO dto){
        roleService.save(dto);
    }

    /**
     * 更新
     *
     * @param dto dto
     */
    @Authorize("role:update")
    @PutMapping
    public void update(@Validated @RequestBody RoleUpdateDTO dto){
        roleService.update(dto);
    }

    /**
     * 删除
     *
     * @param ids id
     */
    @Authorize("role:cancelNewsCollect")
    @DeleteMapping
    public void deleteByIds(@RequestBody List<Long> ids){
        roleService.deleteByIds(ids);
    }

    /**
     * 列表
     *
     * @return {@link List}<{@link RolePageVO}>
     */
    @Authorize("role:page")
    @GetMapping("/list")
    public List<RolePageVO> listPage() {
        return roleService.listPage();
    }

    /**
     * 获取详情
     *
     * @param id id
     * @return {@link IPage}<{@link RolePageVO}>
     */
    @Authorize("role:page")
    @GetMapping("/{id}")
    public RoleDetailVO getDetail(@PathVariable Long id) {
        return roleService.getDetail(id);
    }

    /**
     * 新增角色时的菜单树
     *
     * @return {@link List}<{@link MenuRoleTreeVO}>
     */
    @Authorize("role:save")
    @GetMapping("/menu-tree")
    public List<MenuRoleTreeVO> treeAll() {
        return menuService.treeAll();
    }

    /**
     * 获取所有角色列表
     *
     * @return {@link List}<{@link RoleSimpleVO}>
     */
    @Authorize("user:page")
    @GetMapping("/list-all")
    public List<RoleSimpleVO> listAll() {
        return roleService.listAll();
    }

}
